package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 霍森彪
 * @date 2022/7/26 14:28
 */
@Slf4j
@Service
public class WmSensitiveImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult sensitiveSave(WmSensitive sensitive) {
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "参数为空");
        }
        //获取前端传入的敏感词
        String sensitives = sensitive.getSensitives();
        if (StringUtils.isEmpty(sensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "前端传入得到敏感词为空");
        }
        //根基前端传入的敏感词判断是否重复
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives, sensitives);
        Integer count = wmSensitiveMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已经存在,无法保存");
        }
        sensitive.setCreatedTime(new Date());
        boolean result = this.save(sensitive);

        return result ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public PageResponseResult sensitiveList(SensitiveDto dto) {
        if (dto == null) {
            return new PageResponseResult(0, 0, 0);
        }
        Integer pageNum = dto.getPage();
        Integer pageSize = dto.getSize();
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        String name = dto.getName();
        Page<WmSensitive> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, WmSensitive::getSensitives, name);
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        //组装分页对象
        Page<WmSensitive> selectPage = wmSensitiveMapper.selectPage(page, wrapper);
        Long total = selectPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(pageNum, pageSize, total.intValue());
        pageResponseResult.setData(selectPage.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive sensitive) {
        if (sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数为空");
        }
        Integer id = sensitive.getId();
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"前端传入id为空");
        }
        String sensitives = sensitive.getSensitives();
        if (StringUtils.isEmpty(sensitives)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"前端传入敏感词为空");
        }
        int result = wmSensitiveMapper.updateById(sensitive);
        if (result == 1){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"更新失败");
    }

    @Override
    public ResponseResult deleteSensitive(Integer id) {
        if (id == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"前端传入id为空");
        }
        boolean result = this.removeById(id);
        return result ?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
